package com.grq.framework.core.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Grq工具类
 */
public class GrqUtil {

    /**
     * 获取上传文件的扩展名（小写）
     * @param file
     * @return
     */
    public static String getFileSuffix(MultipartFile file) {
        String name = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(name);
        if (suffix != null){
            suffix = suffix.toLowerCase();
        }
        // String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
        return suffix;
    }

    /**
     * 整型数组转换为字符串数组
     * @param ids
     * @return
     */
    public static Set<String> longs2Strs(Collection<Long> ids){
        if (ids == null){
            return new HashSet<>();
        }
        Set<String> strings = new HashSet<>();
        for (Long id : ids) {
            strings.add(id+"");
        }
        return strings;
    }

    /**
     * 字符串数组转换为整型数组
     * @param strs
     * @return
     */
    public static Set<Long> strs2longs(Collection<String> strs){
        if (strs == null){
            return new HashSet<>();
        }
        Set<Long> ids = new HashSet<>();
        for (String s : strs) {
            ids.add(Long.valueOf(s));
        }
        return ids;
    }

    /**
     * 比较两个月份的时间 (2个时间必须都存在)
     * 如果date1 < date2，返回数小于0，date1==date2返回0，date1 > date2 大于0
     * @param date1
     * @param date2
     * @return
     */
    public static int compare2StringMonth(String date1, String date2){
        String dateStyle = "yyyy-MM";
        Date d1 = DateUtil.parse(date1,dateStyle);
        Date d2 = DateUtil.parse(date2,dateStyle);
        return DateUtil.compare(d1,d2);
    }




    public static void main(String[] args) {
        /*List<TreeNode<String>> nodeList = new ArrayList<>();
        nodeList.add(new TreeNode<Long>("1", "0", "系统管理", 5));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", null));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));

        // 0表示最顶层的id是0
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");




        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setIdKey("name");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setWeight(treeNode.getWeight());
            tree.setName(treeNode.getName());
            // 扩展属性 ...
            tree.putExtra("extraField", 666);
            tree.putExtra("other", new Object());
        });*/

    }


}
